package thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * \* User: Administrator
 * <p>
 * \* Date: 2020/11/17
 * <p>
 * \* Time: 14:29
 * <p>
 * \* Description:
 *      Lock - condition 可以实现 synchronized - wait - notify的功能
 *      conditon必须从Lock对象获取
 *      conditon.await
 *      conditon.signal和conditon.signalAll
 * \
 */
public class LockConditionDemo {

    private static final ReentrantLock lock = new ReentrantLock();
    private static final Condition condition = lock.newCondition();

    public static void main(String[] args) {
        new Thread1().start();
        new Thread2().start();
    }

    static class Thread1 extends Thread{
        @Override
        public void run() {
            lock.lock();
            System.out.println("Thread1 执行");
            try {
                System.out.println("Thread1 await");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread1 执行完毕 释放锁");
            lock.unlock();
        }
    }
    static class Thread2 extends Thread{
        @Override
        public void run() {
            lock.lock();
            System.out.println("Thread2 执行");
            System.out.println("Thread2 signalAll");
            condition.signalAll();
            System.out.println("Thread2 执行完毕 释放锁");
            lock.unlock();
        }
    }
}
